/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.admin.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.action.AbstractAction;
import etcore.webapp.grouptalk.common.config.Config;
import etcore.webapp.grouptalk.common.config.MessageNamespaces;
import etcore.webapp.grouptalk.common.config.Messages;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.common.object.Group;
import etcore.webapp.grouptalk.common.object.Tag;
import etcore.webapp.grouptalk.common.object.TagContainer;
import etcore.webapp.grouptalk.common.object.Topic;
import etcore.webapp.grouptalk.common.service.GroupService;
import etcore.webapp.grouptalk.common.service.MessageService;
import etcore.webapp.grouptalk.common.service.TagContainerService;
import etcore.webapp.grouptalk.common.service.TagService;
import etcore.webapp.grouptalk.common.service.TopicService;
import etcore.webapp.grouptalk.user.object.User;
import etcore.webapp.grouptalk.util.DateUtil;
import etcore.webapp.grouptalk.util.Symbols;

/**
 * This action validates the request and executes the command of managing
 * topic,then set the corresponding attributes for output
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class ManageTopicAction extends AbstractAction {
 
	private static final long serialVersionUID = 2035675433459304564L;
	// validate result
	public static final int ACCESS_ALLOWED = 0;
	public static final int ERROR = 1;

	// parameter/attributes constants

	public static final String CMD_ADD_TOPIC = "addTopic";
	public static final String CMD_EDIT_TOPIC = "editTopic";
	public static final String CMD_DEL_TOPIC = "delTopic";
	public static final String CMD_SORT_TOPICS = "sortTopics";

	public static final String CMD_ADD_TAGCONTAINER = "addTagContainer";

	public static final String CMD_EDIT_TAGCONTAINER = "editTagContainer";

	public static final String CMD_DEL_TAGCONTAINER = "delTagContainer";

	public static final String CMD_SORT_TAGCONTAINERS = "sortTagContainers";

	public static final String CMD_ADD_TAG = "addTag";

	public static final String CMD_EDIT_TAG = "editTag";

	public static final String CMD_DEL_TAG = "delTag";

	public static final String CMD_SORT_TAGS = "sortTags";
	// utility constants
	public static final String RANKV = "rankv";
	public static final String SOURCE_RANKV = "sourceRankv";

	@Override
	public int validate(HttpServletRequest request, HttpServletResponse response) {

		boolean isAdmin = (Boolean) request.getAttribute(PageConstants.ISADMIN);
		GroupService groupService = new GroupService();
		String gid = request.getParameter(PageConstants.ID);
		Group group = null;
		if (gid != null) {
			group = groupService.findById(new Integer(gid));
		}
		if (group != null && isAdmin) {
			request.setAttribute(PageConstants.GROUP, group);

			return ACCESS_ALLOWED;
		}

		return ERROR;

	}

	@Override
	public String getForward(HttpServletRequest request, int validationResult) {
		if (ERROR == validationResult)
			return PageConstants.ACTION_PATH_ERROR;
		return PageConstants.PAGE_PATH_MANAGETOPIC;
	}

	@Override
	public String getRedirect(HttpServletRequest request, int validationResult) {
		// TODO Auto-generated method stub

		return null;
	}

	@Override
	public void execute(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {
		if (ACCESS_ALLOWED == validationResult) {
			delTopic(request);
			addTopic(request);
			editTopic(request);
			sortTopics(request);
			getTopics(request);

			delTag(request);
			addTag(request);
			editTag(request);
			sortTags(request);

			delTagContainer(request);
			addTagContainer(request);
			editTagContainer(request);
			sortTagContainers(request);
			getTagContainers(request);

			getGroups(request);
		}
		// TODO Auto-generated method stub

	}

	private void getGroups(HttpServletRequest request) {
		List<Group> groupList = null;
		int groupNum = 50;
		String groupNumStr = request.getParameter(PageConstants.GROUPNUM);
		if (groupNumStr != null && !Symbols.BLANK.equals(groupNumStr))
			groupNum = Integer.parseInt(groupNumStr);
		GroupService groupService = new GroupService();
		Group group = (Group) request.getAttribute(PageConstants.GROUP);
		HttpSession session = request.getSession(true);
		String lang = (String) session.getAttribute(Messages.LANG);
		if (Messages.LANG_EN.equals(lang)) {
			if (group.getOpen() == 1 || group.getOpen() == 2)
				groupList = groupService.findBy3Properties(PageConstants.OPEN,
						group.getOpen(), Messages.LANG_EN, new Integer(1),
						PageConstants.NAV, group.getNav(), 0, groupNum);
			else
				groupList = groupService.findBy2Properties(PageConstants.OPEN,
						group.getOpen(), Messages.LANG_EN, new Integer(1), 0,
						groupNum);
		} else {
			if (group.getOpen() == 1 || group.getOpen() == 2)
				groupList = groupService.findBy3Properties(PageConstants.OPEN,
						group.getOpen(), Messages.LANG_LOCAL, new Integer(1),
						PageConstants.NAV, group.getNav(), 0, groupNum);
			else
				groupList = groupService.findBy2Properties(PageConstants.OPEN,
						group.getOpen(), Messages.LANG_LOCAL, new Integer(1),
						0, groupNum);

		}
		request.setAttribute(PageConstants.GROUPS, groupList);
	}

	private void getTopics(HttpServletRequest request) {
		TopicService topicService = new TopicService();
		MessageService messageService = new MessageService();
		String gid = request.getParameter(PageConstants.ID);
		List<Topic> topics = topicService.findByProperty(PageConstants.GID,
				new Integer(gid));
		for (Topic topic : topics) {

			long size = messageService.findSizeByProperty(PageConstants.TID,
					topic.getId());
			request.setAttribute(topic.getId() + PageConstants.MSGSIZE, size);
		}
		request.setAttribute(PageConstants.TOPICS, topics);
		request.setAttribute(PageConstants.TOPICSIZE, topics.size());

	}

	private void sortTopics(HttpServletRequest request) {
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_SORT_TOPICS.equals(cmd)) {
			TopicService topicService = new TopicService();

			String gid = request.getParameter(PageConstants.ID);
			List<Topic> topics = topicService.findByProperty(PageConstants.GID,
					new Integer(gid));

			String sourceRankv = request.getParameter(SOURCE_RANKV);
			String rankv = request.getParameter(RANKV);
			if (sourceRankv != null && rankv != null) {
				int sourceRankvInt = Integer.parseInt(sourceRankv);
				int rankvInt = Integer.parseInt(rankv);
				if (sourceRankvInt <= topics.size()
						&& rankvInt <= topics.size()) {
					for (int tmpCount = 0; tmpCount < topics.size(); tmpCount++) {
						Topic tmpGtopic = (Topic) topics.get(tmpCount);
						if (tmpCount != tmpGtopic.getRankv() - 1) {
							tmpGtopic.setRankv(tmpCount + 1);
							topicService.merge(tmpGtopic);
						}
					}
					if (sourceRankvInt < rankvInt) {
						for (int tmpCount = sourceRankvInt + 1; tmpCount <= rankvInt; tmpCount++) {
							Topic tmpGtopic = (Topic) topics.get(tmpCount - 1);
							tmpGtopic.setRankv(tmpGtopic.getRankv() - 1);
							topicService.merge(tmpGtopic);
						}
					} else if (sourceRankvInt > rankvInt) {
						for (int tmpCount = rankvInt; tmpCount <= sourceRankvInt - 1; tmpCount++) {
							Topic tmpGtopic = (Topic) topics.get(tmpCount - 1);
							tmpGtopic.setRankv(tmpGtopic.getRankv() + 1);
							topicService.merge(tmpGtopic);
						}
					}
					Topic sourceTopic = (Topic) topics.get(sourceRankvInt - 1);

					if (sourceTopic != null) {
						sourceTopic.setRankv(rankvInt);

						topicService.merge(sourceTopic);

					}

				}
			}
		}

	}

	private void addTopic(HttpServletRequest request) {
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_ADD_TOPIC.equals(cmd)) {
			HttpSession session = request.getSession(true);
			User user = (User) session.getAttribute(PageConstants.SESSIONUSER);
			String name = request.getParameter(PageConstants.NAME);
			String gid = request.getParameter(PageConstants.ID);
			if (gid != null && name != null && user != null) {
				Topic topic = new Topic();
				TopicService topicService = new TopicService();
				List<Topic> topicList = topicService.findBy2Properties(
						PageConstants.GID, new Integer(gid),
						PageConstants.NAME, name);
				String msg = null;
				String lang = (String) session.getAttribute(Messages.LANG);
				if (topicList != null && topicList.size() > 0) {
					if (lang != null && lang.equals(Messages.LANG_EN)) {
						msg = Messages.getELabel(Messages.MSG_NAME_CONFLICT);
					} else {
						msg = Messages.getLabel(Messages.MSG_NAME_CONFLICT);
					}
					request.setAttribute(PageConstants.MSG, msg);
				} else {
					topic.setName(name);

					topic.setGid(new Integer(gid));
					topic.setParent(new Integer(0));
					topic.setUserName(user.getUserName());
					topic.setRankv((int) topicService.findSizeByProperty(
							PageConstants.GID, new Integer(gid)) + 1);
					topic.setTime(DateUtil.getTime(Config.getProperty(Config.HOURS_DIFFERENCE)));
					topicService.save(topic);
					if (lang != null && lang.equals(Messages.LANG_EN)) {
						msg = Messages.getELabel(Messages.MSG_SUCCESS);
					} else {
						msg = Messages.getLabel(Messages.MSG_SUCCESS);
					}
					request.setAttribute(PageConstants.MSG, msg);
				}
			}
		}

	}

	private void editTopic(HttpServletRequest request) {
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_EDIT_TOPIC.equals(cmd)) {
			String id = request.getParameter(PageConstants.TID);
			String destinationTopicId = request
					.getParameter("destinationTopicId");
			String name = request.getParameter(PageConstants.NAME);
			HttpSession session = request.getSession(true);
			User user = (User) session.getAttribute(PageConstants.SESSIONUSER);
			TopicService topicService = new TopicService();
			String msg = null;
			String lang = (String) session.getAttribute(Messages.LANG);
			if (id != null && user != null) {
				Topic topic = topicService.findById(new Integer(id));
				if (name != null && destinationTopicId == null) {

					if (topic != null) {
						if (!topic.getName().equals(name)) {
							List<Topic> topicList = topicService.findBy2Properties(
									PageConstants.GID, topic.getGid(),
									PageConstants.NAME, name);
							if (topicList != null && topicList.size() > 0) {
								if (lang != null
										&& lang.equals(Messages.LANG_EN)) {
									msg = Messages
											.getELabel(Messages.MSG_NAME_CONFLICT);
								} else {
									msg = Messages
											.getLabel(Messages.MSG_NAME_CONFLICT);
								}
								request.setAttribute(PageConstants.MSG, msg);
								return;
							}
						}
						topic.setName(name);
						topic.setUserName(user.getUserName());

						topicService.merge(topic);

					}

				} else if (id != null && user != null && name == null
						&& destinationTopicId != null) {
					if (topic != null) {
						MessageService messageService = new MessageService();
						messageService.batchTopicTransforming(topic.getId(),
								new Integer(destinationTopicId));

					}
				}

				if (lang != null && lang.equals(Messages.LANG_EN)) {
					msg = Messages.getELabel(Messages.MSG_SUCCESS);
				} else {
					msg = Messages.getLabel(Messages.MSG_SUCCESS);
				}
				request.setAttribute(PageConstants.MSG, msg);

			}
		}
	}

	private void delTopic(HttpServletRequest request) {
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_DEL_TOPIC.equals(cmd)) {
			String id = request.getParameter(PageConstants.TID);

			HttpSession session = request.getSession(true);
			User user = (User) session.getAttribute(PageConstants.SESSIONUSER);
			TopicService topicService = new TopicService();
			String msg = null;
			String lang = (String) session.getAttribute(Messages.LANG);
			if (id != null && user != null) {
				Topic topic = topicService.findById(new Integer(id));
				if (topic != null) {
					topicService.deleteSingle(topic);

					if (lang != null && lang.equals(Messages.LANG_EN)) {
						msg = Messages.getELabel(Messages.MSG_SUCCESS);
					} else {
						msg = Messages.getLabel(Messages.MSG_SUCCESS);
					}
					request.setAttribute(PageConstants.MSG, msg);
				}
			}
		}

	}

	private void addTagContainer(HttpServletRequest request) {
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_ADD_TAGCONTAINER.equals(cmd)) {
			HttpSession session = request.getSession(true);
			User user = (User) session.getAttribute(PageConstants.SESSIONUSER);
			String name = request.getParameter(PageConstants.NAME);
			String gid = request.getParameter(PageConstants.ID);
			if (gid != null && name != null && user != null) {

				TagContainerService tagContainerService = new TagContainerService();
				List<TagContainer> tagContainerList = tagContainerService.findBy2Properties(
						PageConstants.GID, new Integer(gid),
						PageConstants.NAME, name);
				String msg = null;
				String lang = (String) session.getAttribute(Messages.LANG);
				if (tagContainerList != null && tagContainerList.size() > 0) {
					if (lang != null && lang.equals(Messages.LANG_EN)) {
						msg = Messages.getELabel(Messages.MSG_NAME_CONFLICT);
					} else {
						msg = Messages.getLabel(Messages.MSG_NAME_CONFLICT);
					}
					request.setAttribute(PageConstants.MSG, msg);
				} else {
					TagContainer tagContainer = new TagContainer();
					tagContainer.setName(name);

					tagContainer.setGid(new Integer(gid));

					tagContainer.setRankv((int) tagContainerService
							.findSizeByProperty(PageConstants.GID, new Integer(
									gid)) + 1);

					tagContainerService.save(tagContainer);
					if (lang != null && lang.equals(Messages.LANG_EN)) {
						msg = Messages.getELabel(Messages.MSG_SUCCESS);
					} else {
						msg = Messages.getLabel(Messages.MSG_SUCCESS);
					}
					request.setAttribute(PageConstants.MSG, msg);
				}
			}
		}

	}

	private void editTagContainer(HttpServletRequest request) {
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_EDIT_TAGCONTAINER.equals(cmd)) {
			String id = request.getParameter(PageConstants.TAGCONTAINERID);
			String name = request.getParameter(PageConstants.NAME);
			HttpSession session = request.getSession(true);
			User user = (User) session.getAttribute(PageConstants.SESSIONUSER);
			TagContainerService tagContainerService = new TagContainerService();

			String msg = null;
			String lang = (String) session.getAttribute(Messages.LANG);
			if (id != null && user != null && name != null) {

				TagContainer tagContainer = tagContainerService
						.findById(new Integer(id));
				if (tagContainer != null) {
					if (!tagContainer.getName().equals(name)) {
						List<TagContainer> tagContainerList = tagContainerService
								.findBy2Properties(PageConstants.GID,
										tagContainer.getGid(),
										PageConstants.NAME, name);
						if (tagContainerList != null
								&& tagContainerList.size() > 0) {
							if (lang != null && lang.equals(Messages.LANG_EN)) {
								msg = Messages
										.getELabel(Messages.MSG_NAME_CONFLICT);
							} else {
								msg = Messages
										.getLabel(Messages.MSG_NAME_CONFLICT);
							}
							request.setAttribute(PageConstants.MSG, msg);
							return;
						}
					}
					tagContainer.setName(name);

					tagContainerService.merge(tagContainer);
					if (lang != null && lang.equals(Messages.LANG_EN)) {
						msg = Messages.getELabel(Messages.MSG_SUCCESS);
					} else {
						msg = Messages.getLabel(Messages.MSG_SUCCESS);
					}
					request.setAttribute(PageConstants.MSG, msg);
				}

			}

		}

	}

	private void getTagContainers(HttpServletRequest request) {
		TagContainerService tagContainerService = new TagContainerService();

		String gid = request.getParameter(PageConstants.ID);
		List<TagContainer> tagContainers = tagContainerService.findByProperty(
				PageConstants.GID, new Integer(gid));
		if (tagContainers != null) {
			request.setAttribute(PageConstants.TAGCONTAINERS, tagContainers);
			request.setAttribute(PageConstants.TAGCONTAINERCOUNT,
					tagContainers.size());
			TagService tagService = new TagService();
			for (TagContainer tagContainer : tagContainers) {

				List<Tag> tags = tagService.findByProperty(
						PageConstants.TAGCONTAINERID, tagContainer.getId());
				if (tags != null) {
					request.setAttribute(tagContainer.getId()
							+ PageConstants.TAGS, tags);
					request.setAttribute(tagContainer.getId()
							+ PageConstants.TAGCOUNT, tags.size());
				}
			}

		}
	}

	private void sortTagContainers(HttpServletRequest request) {
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_SORT_TAGCONTAINERS.equals(cmd)) {
			TagContainerService tagContainerService = new TagContainerService();

			String gid = request.getParameter(PageConstants.ID);
			List<TagContainer> tagContainers = tagContainerService
					.findByProperty(PageConstants.GID, new Integer(gid));

			String sourceRankv = request.getParameter(SOURCE_RANKV);
			String rankv = request.getParameter(RANKV);
			if (sourceRankv != null && rankv != null) {
				int sourceRankvInt = Integer.parseInt(sourceRankv);
				int rankvInt = Integer.parseInt(rankv);
				if (sourceRankvInt <= tagContainers.size()
						&& rankvInt <= tagContainers.size()) {
					for (int tmpCount = 0; tmpCount < tagContainers.size(); tmpCount++) {
						TagContainer tmpTagContainer = (TagContainer) tagContainers
								.get(tmpCount);
						if (tmpCount != tmpTagContainer.getRankv() - 1) {
							tmpTagContainer.setRankv(tmpCount + 1);
							tagContainerService.merge(tmpTagContainer);
						}
					}
					if (sourceRankvInt < rankvInt) {
						for (int tmpCount = sourceRankvInt + 1; tmpCount <= rankvInt; tmpCount++) {
							TagContainer tmpTagContainer = (TagContainer) tagContainers
									.get(tmpCount - 1);
							tmpTagContainer
									.setRankv(tmpTagContainer.getRankv() - 1);
							tagContainerService.merge(tmpTagContainer);
						}
					} else if (sourceRankvInt > rankvInt) {
						for (int tmpCount = rankvInt; tmpCount <= sourceRankvInt - 1; tmpCount++) {
							TagContainer tmpTagContainer = (TagContainer) tagContainers
									.get(tmpCount - 1);
							tmpTagContainer
									.setRankv(tmpTagContainer.getRankv() + 1);
							tagContainerService.merge(tmpTagContainer);
						}
					}
					TagContainer sourceTagContainer = (TagContainer) tagContainers
							.get(sourceRankvInt - 1);

					if (sourceTagContainer != null) {
						sourceTagContainer.setRankv(rankvInt);

						tagContainerService.merge(sourceTagContainer);

					}

				}
			}
		}

	}

	private void delTagContainer(HttpServletRequest request) {
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_DEL_TAGCONTAINER.equals(cmd)) {
			String id = request.getParameter(PageConstants.TAGCONTAINERID);

			HttpSession session = request.getSession(true);
			User user = (User) session.getAttribute(PageConstants.SESSIONUSER);
			TagContainerService tagContainerService = new TagContainerService();
			String msg = null;
			String lang = (String) session.getAttribute(Messages.LANG);
			if (id != null && user != null) {
				TagContainer tagContainer = tagContainerService
						.findById(new Integer(id));
				if (tagContainer != null) {
					tagContainerService.deleteSingle(tagContainer);

					if (lang != null && lang.equals(Messages.LANG_EN)) {
						msg = Messages.getELabel(Messages.MSG_SUCCESS);
					} else {
						msg = Messages.getLabel(Messages.MSG_SUCCESS);
					}
					request.setAttribute(PageConstants.MSG, msg);
				}
			}
		}

	}

	private void addTag(HttpServletRequest request) {
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_ADD_TAG.equals(cmd)) {
			HttpSession session = request.getSession(true);
			User user = (User) session.getAttribute(PageConstants.SESSIONUSER);
			String name = request.getParameter(PageConstants.NAME);
			String containerId = request
					.getParameter(PageConstants.TAGCONTAINERID);
			if (containerId != null && name != null && user != null) {

				TagService tagService = new TagService();
				Integer tcid = new Integer(containerId);
				List<Tag> tagList = tagService.findBy2Properties(
						PageConstants.TAGCONTAINERID, tcid, PageConstants.NAME,
						name);
				String msg = null;
				String lang = (String) session.getAttribute(Messages.LANG);
				if (tagList != null && tagList.size() > 0) {
					if (lang != null && lang.equals(Messages.LANG_EN)) {
						msg = Messages.getELabel(Messages.MSG_NAME_CONFLICT);
					} else {
						msg = Messages.getLabel(Messages.MSG_NAME_CONFLICT);
					}
					request.setAttribute(PageConstants.MSG, msg);
				} else {
					Tag tag = new Tag();
					tag.setName(name);
					tag.setTcid(tcid);

					tag.setRankv((int) tagService.findSizeByProperty(
							PageConstants.TAGCONTAINERID, tcid) + 1);

					tagService.save(tag);
					if (lang != null && lang.equals(Messages.LANG_EN)) {
						msg = Messages.getELabel(Messages.MSG_SUCCESS);
					} else {
						msg = Messages.getLabel(Messages.MSG_SUCCESS);
					}
					request.setAttribute(PageConstants.MSG, msg);
				}
			}
		}

	}

	private void editTag(HttpServletRequest request) {
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_EDIT_TAG.equals(cmd)) {
			String id = request.getParameter(PageConstants.TAGID);
			String name = request.getParameter(PageConstants.NAME);
			HttpSession session = request.getSession(true);
			User user = (User) session.getAttribute(PageConstants.SESSIONUSER);
			TagService tagService = new TagService();

			String msg = null;
			String lang = (String) session.getAttribute(Messages.LANG);
			if (id != null && user != null && name != null) {

				Tag tag = tagService.findById(new Integer(id));
				if (tag != null) {
					if (!tag.getName().equals(name)) {
						List<Tag> tagList = tagService.findBy2Properties(
								PageConstants.TAGCONTAINERID, tag.getTcid(),
								PageConstants.NAME, name);
						if (tagList != null && tagList.size() > 0) {
							if (lang != null && lang.equals(Messages.LANG_EN)) {
								msg = Messages
										.getELabel(Messages.MSG_NAME_CONFLICT);
							} else {
								msg = Messages
										.getLabel(Messages.MSG_NAME_CONFLICT);
							}
							request.setAttribute(PageConstants.MSG, msg);
							return;
						}
					}
					tag.setName(name);

					tagService.merge(tag);
					if (lang != null && lang.equals(Messages.LANG_EN)) {
						msg = Messages.getELabel(Messages.MSG_SUCCESS);
					} else {
						msg = Messages.getLabel(Messages.MSG_SUCCESS);
					}
					request.setAttribute(PageConstants.MSG, msg);
				}

			}

		}

	}

	private void sortTags(HttpServletRequest request) {

		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_SORT_TAGS.equals(cmd)) {
			String tcid = request.getParameter(PageConstants.TAGCONTAINERID);

			String sourceRankv = request.getParameter(SOURCE_RANKV);
			String rankv = request.getParameter(RANKV);
			TagService tagService = new TagService();

			List<Tag> tags = tagService.findByProperty(
					PageConstants.TAGCONTAINERID, new Integer(tcid));

			if (sourceRankv != null && rankv != null) {
				int sourceRankvInt = Integer.parseInt(sourceRankv);
				int rankvInt = Integer.parseInt(rankv);
				if (sourceRankvInt <= tags.size() && rankvInt <= tags.size()) {
					for (int tmpCount = 0; tmpCount < tags.size(); tmpCount++) {
						Tag tmpTag = (Tag) tags.get(tmpCount);
						if (tmpCount != tmpTag.getRankv() - 1) {
							tmpTag.setRankv(tmpCount + 1);
							tagService.merge(tmpTag);
						}
					}
					if (sourceRankvInt < rankvInt) {
						for (int tmpCount = sourceRankvInt + 1; tmpCount <= rankvInt; tmpCount++) {
							Tag tmpTag = (Tag) tags.get(tmpCount - 1);
							tmpTag.setRankv(tmpTag.getRankv() - 1);
							tagService.merge(tmpTag);
						}
					} else if (sourceRankvInt > rankvInt) {
						for (int tmpCount = rankvInt; tmpCount <= sourceRankvInt - 1; tmpCount++) {
							Tag tmpTag = (Tag) tags.get(tmpCount - 1);
							tmpTag.setRankv(tmpTag.getRankv() + 1);
							tagService.merge(tmpTag);
						}
					}
					Tag sourceTag = (Tag) tags.get(sourceRankvInt - 1);

					if (sourceTag != null) {
						sourceTag.setRankv(rankvInt);

						tagService.merge(sourceTag);

					}

				}
			}

		}
	}

	private void delTag(HttpServletRequest request) {
		String cmd = request.getParameter(PageConstants.CMD);
		if (CMD_DEL_TAG.equals(cmd)) {
			String id = request.getParameter(PageConstants.TAGID);

			HttpSession session = request.getSession(true);
			User user = (User) session.getAttribute(PageConstants.SESSIONUSER);
			TagService tagService = new TagService();
			String msg = null;
			String lang = (String) session.getAttribute(Messages.LANG);
			if (id != null && user != null) {
				Tag tag = tagService.findById(new Integer(id));
				if (tag != null) {
					tagService.deleteSingle(tag);

					if (lang != null && lang.equals(Messages.LANG_EN)) {
						msg = Messages.getELabel(Messages.MSG_SUCCESS);
					} else {
						msg = Messages.getLabel(Messages.MSG_SUCCESS);
					}
					request.setAttribute(PageConstants.MSG, msg);
				}
			}
		}

	}

	@Override
	public boolean isForward(int validationResult) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean enablePostLoad() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see etcore.webapp.grouptalk.controller.action.AbstractAction#buildSelfContextAndAttributes(int, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public List<String> getNamespacesOfMessages(HttpServletRequest request) {
		List<String> namespaces = new ArrayList<String>();
		namespaces.add(MessageNamespaces.GROUP_ADMIN);
		return namespaces;
	}

}
